﻿// -----------------------------------------------------------------------
//  <copyright file="GetSettingByTypeNameQuery.cs" company="LiuliuSoft">
//      Copyright (c) 2025 66SOFT. All rights reserved.
//  </copyright>
//  <site>https://ifs.66soft.net</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-10-08 16:10</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Authorization;
using DaprPlus.Domain.Entity;
using DaprPlus.Mapping;
using DaprPlus.MediatR;
using DaprPlus.Settings.Domain.Dtos;
using DaprPlus.Settings.Domain.Entities;
using DaprPlus.Settings.Domain.Services;
using DaprPlus.Settings.Domain.Specs;


namespace DaprPlus.Settings.Application.Queries;

[AuthConfig(typeof(SettingDefinition), ResourceAccessType.RoleLimit, "GetSetting")]
public class GetSettingByTypeNameQuery(string typeName) : SingleQuery<SettingDefinitionOutDto?>
{
    public string TypeName { get; set; } = typeName;
}

public class GetSettingByTypeNameQueryHandler(ISettingStore store) 
    : IRequestHandler<GetSettingByTypeNameQuery, SettingDefinitionOutDto?>
{
    public async Task<SettingDefinitionOutDto?> Handle(GetSettingByTypeNameQuery request, CancellationToken cancellationToken)
    {
        var definition = await store.GetOrCreateSettingDefinitionAsync(request.TypeName, cancellationToken);
        return definition;
    }
}